package org.jeecg.modules.purchase.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.purchase.entity.PurchaseProvider;
import org.jeecg.modules.purchase.entity.PurchaseProviderCertification;
import org.jeecg.modules.purchase.entity.PurchaseProviderContacts;
import org.jeecg.modules.purchase.entity.PurchaseProviderGoods;
import org.jeecg.modules.purchase.mapper.PurchaseProviderCertificationMapper;
import org.jeecg.modules.purchase.mapper.PurchaseProviderContactsMapper;
import org.jeecg.modules.purchase.mapper.PurchaseProviderGoodsMapper;
import org.jeecg.modules.purchase.mapper.PurchaseProviderMapper;
import org.jeecg.modules.purchase.service.IPurchaseProviderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 供应商
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Service
public class PurchaseProviderServiceImpl extends ServiceImpl<PurchaseProviderMapper, PurchaseProvider> implements IPurchaseProviderService {

	@Autowired
	private PurchaseProviderMapper purchaseProviderMapper;
	@Autowired
	private PurchaseProviderCertificationMapper purchaseProviderCertificationMapper;
	@Autowired
	private PurchaseProviderContactsMapper purchaseProviderContactsMapper;
	@Autowired
	private PurchaseProviderGoodsMapper purchaseProviderGoodsMapper;

	@Autowired
	private ISysBaseAPI sysBaseAPI;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(PurchaseProvider purchaseProvider, List<PurchaseProviderCertification> purchaseProviderCertificationList,List<PurchaseProviderContacts> purchaseProviderContactsList,List<PurchaseProviderGoods> purchaseProviderGoodsList) {
		if(StringUtils.isBlank(purchaseProvider.getCode())){
			String code = null;
			Result result = sysBaseAPI.executeByRuleCode("ProviderCodeRule",new JSONObject());
			if(result.isSuccess()){
				code = (String)result.getResult();
			}
			purchaseProvider.setCode(code);
		}
		purchaseProviderMapper.insert(purchaseProvider);
		if(purchaseProviderCertificationList!=null && purchaseProviderCertificationList.size()>0) {
			for(PurchaseProviderCertification entity:purchaseProviderCertificationList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderCertificationMapper.insert(entity);
			}
		}
		if(purchaseProviderContactsList!=null && purchaseProviderContactsList.size()>0) {
			for(PurchaseProviderContacts entity:purchaseProviderContactsList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderContactsMapper.insert(entity);
			}
		}
		if(purchaseProviderGoodsList!=null && purchaseProviderGoodsList.size()>0) {
			for(PurchaseProviderGoods entity:purchaseProviderGoodsList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(PurchaseProvider purchaseProvider,List<PurchaseProviderCertification> purchaseProviderCertificationList,List<PurchaseProviderContacts> purchaseProviderContactsList,List<PurchaseProviderGoods> purchaseProviderGoodsList) {
		purchaseProviderMapper.updateById(purchaseProvider);
		
		//1.先删除子表数据
		purchaseProviderCertificationMapper.deleteByMainId(purchaseProvider.getId());
		purchaseProviderContactsMapper.deleteByMainId(purchaseProvider.getId());
		purchaseProviderGoodsMapper.deleteByMainId(purchaseProvider.getId());
		
		//2.子表数据重新插入
		if(purchaseProviderCertificationList!=null && purchaseProviderCertificationList.size()>0) {
			for(PurchaseProviderCertification entity:purchaseProviderCertificationList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderCertificationMapper.insert(entity);
			}
		}
		if(purchaseProviderContactsList!=null && purchaseProviderContactsList.size()>0) {
			for(PurchaseProviderContacts entity:purchaseProviderContactsList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderContactsMapper.insert(entity);
			}
		}
		if(purchaseProviderGoodsList!=null && purchaseProviderGoodsList.size()>0) {
			for(PurchaseProviderGoods entity:purchaseProviderGoodsList) {
				//外键设置
				entity.setProviderId(purchaseProvider.getId());
				entity.setProviderCode(purchaseProvider.getCode());
				purchaseProviderGoodsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		purchaseProviderCertificationMapper.deleteByMainId(id);
		purchaseProviderContactsMapper.deleteByMainId(id);
		purchaseProviderGoodsMapper.deleteByMainId(id);
		purchaseProviderMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			purchaseProviderCertificationMapper.deleteByMainId(id.toString());
			purchaseProviderContactsMapper.deleteByMainId(id.toString());
			purchaseProviderGoodsMapper.deleteByMainId(id.toString());
			purchaseProviderMapper.deleteById(id);
		}
	}
	
}
